package com.lyf.scm.admin.domain.remote.allocationservice.facade;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;
import com.lyf.scm.admin.api.dto.*;
import com.lyf.scm.admin.domain.remote.allocationservice.ActualSalesQtyRemote;
import com.lyf.scm.admin.domain.remote.allocationservice.RequirementPlanRemote;
import com.lyf.scm.admin.domain.remote.allocationservice.dto.ActualSalesQtyQueryDTO;
import com.lyf.scm.common.enums.ResponseMsg;
import com.rome.arch.core.clientobject.Response;
import com.rome.arch.core.exception.RomeException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * 需求计划
 *
 * @author wangchlei
 * @date 2020/10/26
 */
@Service
@Slf4j
public class RequirementPlanFacade {
    @Resource
    private RequirementPlanRemote requirementPlanRemote;
    @Resource
    private ActualSalesQtyRemote actualSalesQtyRemote;

    public PageInfo<RequirementPlanInfoDTO> listWeekBySelective(RequirementPlanDTO requirementPlanDTO) {
        log.info("按条件查询需求计划(周)入参:RequirementPlanDTO:{}", JSON.toJSONString(requirementPlanDTO));
        Response<PageInfo<RequirementPlanInfoDTO>> response = requirementPlanRemote.listWeekBySelective(requirementPlanDTO);
        if (!response.getCode().equals(ResponseMsg.SUCCESS.getCode())) {
            throw new RomeException(response.getCode(), response.getMsg());
        }
        return response.getData();
    }

    public void updateConfirmQty(List<RequirementConfirmationDTO> requirementConfirmationDTOs,Long unitType) {
        log.info("更新供应链确认量:requirementConfirmationDTOs:{}", JSON.toJSONString(requirementConfirmationDTOs));
        Response response = requirementPlanRemote.updateConfirmQty(requirementConfirmationDTOs,unitType);
        if (!response.getCode().equals(ResponseMsg.SUCCESS.getCode())) {
            throw new RomeException(response.getCode(), response.getMsg());
        }
    }

    public EChartsDTO listAllByCycles(RequirementPlanDTO requirementPlanDTO) {
        log.info("图表数据接口入参:requirementPlanDTO:{}", JSON.toJSONString(requirementPlanDTO));
        Response<EChartsDTO> response = requirementPlanRemote.listAllByCycles(requirementPlanDTO);
        if (!response.getCode().equals(ResponseMsg.SUCCESS.getCode())) {
            throw new RomeException(response.getCode(), response.getMsg());
        }
        return response.getData();
    }

    public feign.Response exportExcel(RequirementPlanDTO requirementPlanDTO, Integer type) {
        log.info("需求计划导出excel入参:requirementPlanDTO:{}，type:{}", JSON.toJSONString(requirementPlanDTO),type);
        return requirementPlanRemote.exportExcel(requirementPlanDTO, type);
    }

    public Map<String, List<BigDecimal>> groupByChannelAreaSku(ActualSalesQtyQueryDTO dto) {
        log.info("实际销量入参:dto:{}", JSON.toJSONString(dto));
        Response<Map<String, List<BigDecimal>>> response = actualSalesQtyRemote.groupByChannelAreaSku(dto);
        if (!response.getCode().equals(ResponseMsg.SUCCESS.getCode())) {
            throw new RomeException(response.getCode(), response.getMsg());
        }
        return response.getData();
    }
}
